package com.lechi.web.listener;


import com.lechi.web.framework.log.IotExceptionLog;
import com.lechi.web.service.IIotFtpUploadsService;
import com.lechi.web.system.domain.IotFtpUploads;
import jakarta.annotation.Resource;
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.concurrent.CompletableFuture;

/**
 * The type Ftp upload file listener.
 */
@Component
public class FtpUploadFileListener extends FileAlterationListenerAdaptor {
    @Resource
    private IIotFtpUploadsService iotFtpUploadsService;

    @Override
    public void onFileDelete(File file) {
        String compressedPath = file.getAbsolutePath();
        try {
            IotFtpUploads iotFtpUploads = new IotFtpUploads();
            iotFtpUploads.setFilepath(compressedPath);
            iotFtpUploadsService.deleteIotFtpUploadsByParams(iotFtpUploads);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "Ftp文件监听删除文件服务异常", method, compressedPath));
        }
    }
}
